package com.basics.common.utils.file;


import com.basics.common.exception.file.FileNameLengthLimitExceededException;
import com.basics.common.utils.GuidUtils;
import com.basics.common.utils.Md5Utils;
import com.basics.framework.config.BasicsConfig;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class FileUploadUtils {
    public static final long DEFAULT_MAX_SIZE = 52428800L;
    private static String defaultBaseDir = BasicsConfig.getProfile();
    public static final int DEFAULT_FILE_NAME_LENGTH = 200;
    public static final String IMAGE_JPG_EXTENSION = ".jpg";
    private static int counter = 0;

    public FileUploadUtils() {
    }

    public static void setDefaultBaseDir(String defaultBaseDir) {
        defaultBaseDir = defaultBaseDir;
    }

    public static String getDefaultBaseDir() {
        return defaultBaseDir;
    }

    public static final String upload(MultipartFile file) throws IOException {
        try {
            return upload(getDefaultBaseDir(), file, ".jpg");
        } catch (Exception var2) {
            throw new IOException(var2);
        }
    }

    public static final String upload(String baseDir, MultipartFile file) throws IOException {
        try {
            return upload(baseDir, file, ".jpg");
        } catch (Exception var3) {
            throw new IOException(var3);
        }
    }

    public static final String upload(String baseDir, MultipartFile file, String extension) throws Exception, IOException, FileNameLengthLimitExceededException {
        int fileNamelength = file.getOriginalFilename().length();
        if (fileNamelength > 200) {
            throw new FileNameLengthLimitExceededException(file.getOriginalFilename(), fileNamelength, 200);
        } else {
            assertAllowed(file);
            String fileName = encodingFilename(file.getOriginalFilename(), extension);
            String url = "http://www.bulanwa.cn/" + baseDir.substring(baseDir.indexOf("upload"), baseDir.length()) + fileName;
            File desc = getAbsoluteFile(baseDir, baseDir + fileName);
            file.transferTo(desc);
            return url;
        }
    }

    public static final String uploadByte(String baseDir, byte[] bs, String extension) throws Exception, IOException, FileNameLengthLimitExceededException {
        String fileName = GuidUtils.generateSimpleGuid() + extension;
        String url = "http://www.bulanwa.cn/" + baseDir.substring(baseDir.indexOf("upload"), baseDir.length()) + fileName;
        File desc = getAbsoluteFile(baseDir, baseDir + fileName);
        FileOutputStream fos = new FileOutputStream(desc);
        fos.write(bs, 0, bs.length);
        fos.flush();
        fos.close();
        return url;
    }

    private static final File getAbsoluteFile(String uploadDir, String filename) throws IOException {
        File desc = new File(File.separator + filename);
        if (!desc.getParentFile().exists()) {
            desc.getParentFile().mkdirs();
        }

        if (!desc.exists()) {
            desc.createNewFile();
        }

        return desc;
    }

    private static final String encodingFilename(String filename, String extension) {
        filename = filename.replace("_", " ");
        filename = Md5Utils.hash(filename + System.nanoTime() + counter++) + extension;
        return filename;
    }

    public static final void assertAllowed(MultipartFile file) throws Exception {
        long size = file.getSize();
        if (size > 52428800L) {
            throw new Exception("not allowed upload upload");
        }
    }
}
